package com.example.nettydemo.codec.ptotobuf;

import com.alibaba.fastjson.JSON;
import com.example.nettydemo.dto.UserInfo;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.msgpack.MessagePack;

@Slf4j
public class SubReqServerHandler extends ChannelHandlerAdapter {

    private int counter;

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        SubscibeReqProto.SubscibeReq req = (SubscibeReqProto.SubscibeReq) msg;
        log.info("request: [{}], counter: [{}]", req.toString(), ++counter);

        ctx.writeAndFlush(initSubscibeResp(req));
    }

    private SubscibeRespProto.SubscibeResp initSubscibeResp(SubscibeReqProto.SubscibeReq req){
        SubscibeRespProto.SubscibeResp.Builder builder = SubscibeRespProto.SubscibeResp.newBuilder();
        builder.setSubReqID(req.getSubReqID());
        builder.setRespCode(200);
        builder.setDesc("成功");

        return builder.build();
    }
}
